﻿using System.Collections.Generic;
using System.Threading.Tasks;
using WeiboWPSdk.Json;
using WeiboWPSdk.ResponseEntity;

namespace WeiboWPSdk.Api {
    public class StatusApi : ApiBase {
        public StatusApi(ClientHttpHelper httpHelper)
            : base(httpHelper) {
        }

        #region 读取接口
        /// <summary>
        /// 返回最新的200条公共微博，返回结果非完全实时
        /// http://open.weibo.com/wiki/2/statuses/public_timeline
        /// </summary>
        /// <param name="count">单页返回的记录条数，最大不超过200，默认为20。</param>
        /// <returns></returns>
        public async Task<StatusCollection> GetPublicTimelineAsync(int count = 20) {
            var json = await HttpHelper.GetBasicCommandAsync("statuses/public_timeline", new WeiboParameter("count", count));
            return JsonConvert.DeserializeObject<StatusCollection>(json);
        }
        /// <summary>
        /// 获取当前登录用户及其所关注用户的最新微博
        /// http://open.weibo.com/wiki/2/statuses/friends_timeline
        /// </summary>
        /// <param name="sinceId">若指定此参数，则返回ID比since_id大的微博（即比since_id时间晚的微博），默认为0。</param>
        /// <param name="maxId">若指定此参数，则返回ID小于或等于max_id的微博，默认为0。</param>
        /// <param name="count">单页返回的记录条数，最大不超过100，默认为20。</param>
        /// <param name="page">返回结果的页码，默认为1。</param>
        /// <param name="baseApp">是否只获取当前应用的数据。0为否（所有数据），1为是（仅当前应用），默认为0。</param>
        /// <param name="feature">过滤类型ID，0：全部、1：原创、2：图片、3：视频、4：音乐，默认为0。</param>
        /// <param name="trimUser">返回值中user字段开关，0：返回完整user字段、1：user字段仅返回user_id，默认为0。</param>
        /// <returns></returns>
        public async Task<StatusCollection> GetFriendsTimelineAsync(long sinceId = 0, long maxId = 0, int count = 20, int page = 1, bool baseApp = false, int feature = 0, bool trimUser = false) {
            var json = await HttpHelper.GetBasicCommandAsync("statuses/friends_timeline", new WeiboParameter("since_id", sinceId), new WeiboParameter("max_id", maxId),
                new WeiboParameter("count", count), new WeiboParameter("page", page), new WeiboParameter("base_app", baseApp),
                new WeiboParameter("feature", feature), new WeiboParameter("trim_user", trimUser));
            return JsonConvert.DeserializeObject<StatusCollection>(json);
        }
        /// <summary>
        /// 获取当前登录用户及其所关注用户的最新微博
        /// http://open.weibo.com/wiki/2/statuses/home_timeline
        /// </summary>
        /// <param name="sinceId">若指定此参数，则返回ID比since_id大的微博（即比since_id时间晚的微博），默认为0。</param>
        /// <param name="maxId">若指定此参数，则返回ID小于或等于max_id的微博，默认为0。</param>
        /// <param name="count">单页返回的记录条数，最大不超过100，默认为20。</param>
        /// <param name="page">返回结果的页码，默认为1。</param>
        /// <param name="baseApp">是否只获取当前应用的数据。0为否（所有数据），1为是（仅当前应用），默认为0。</param>
        /// <param name="feature">过滤类型ID，0：全部、1：原创、2：图片、3：视频、4：音乐，默认为0。</param>
        /// <param name="trimUser">返回值中user字段开关，0：返回完整user字段、1：user字段仅返回user_id，默认为0。</param>
        /// <returns></returns>
        public async Task<StatusCollection> GetHomeTimelineAsync(long sinceId = 0, long maxId = 0, int count = 20, int page = 1, bool baseApp = false, int feature = 0, bool trimUser = false) {
            var json = await HttpHelper.GetBasicCommandAsync("statuses/home_timeline", new WeiboParameter("since_id", sinceId), new WeiboParameter("max_id", maxId),
                new WeiboParameter("count", count), new WeiboParameter("page", page), new WeiboParameter("base_app", baseApp),
                new WeiboParameter("feature", feature), new WeiboParameter("trim_user", trimUser));
            return JsonConvert.DeserializeObject<StatusCollection>(json);
        }
        /// <summary>
        /// 获取当前登录用户及其所关注用户的最新微博的ID
        /// http://open.weibo.com/wiki/2/statuses/friends_timeline/ids
        /// </summary>
        /// <param name="sinceId">若指定此参数，则返回ID比since_id大的微博（即比since_id时间晚的微博），默认为0。</param>
        /// <param name="maxId">若指定此参数，则返回ID小于或等于max_id的微博，默认为0。</param>
        /// <param name="count">单页返回的记录条数，最大不超过100，默认为20。</param>
        /// <param name="page">返回结果的页码，默认为1。</param>
        /// <param name="baseApp">是否只获取当前应用的数据。0为否（所有数据），1为是（仅当前应用），默认为0。</param>
        /// <param name="feature">过滤类型ID，0：全部、1：原创、2：图片、3：视频、4：音乐，默认为0。</param>
        /// <returns></returns>
        public async Task<StatusIdCollection> GetFriendsTimelineIdsAsync(long sinceId = 0, long maxId = 0, int count = 20, int page = 1, bool baseApp = false, int feature = 0) {
            var json = await HttpHelper.GetBasicCommandAsync("statuses/friends_timeline/ids", new WeiboParameter("since_id", sinceId), new WeiboParameter("max_id", maxId),
                new WeiboParameter("count", count), new WeiboParameter("page", page), new WeiboParameter("base_app", baseApp), new WeiboParameter("feature", feature));
            return JsonConvert.DeserializeObject<StatusIdCollection>(json);
        }
        /// <summary>
        /// 获取某个用户最新发表的微博列表
        /// http://open.weibo.com/wiki/2/statuses/user_timeline
        /// </summary>
        /// <param name="uid">需要查询的用户ID。</param>
        /// <param name="screenName">需要查询的用户昵称。</param>
        /// <param name="sinceId">若指定此参数，则返回ID比since_id大的微博（即比since_id时间晚的微博），默认为0。</param>
        /// <param name="maxId">若指定此参数，则返回ID小于或等于max_id的微博，默认为0。</param>
        /// <param name="count">单页返回的记录条数，最大不超过100，默认为20。</param>
        /// <param name="page">返回结果的页码，默认为1。</param>
        /// <param name="baseApp">是否只获取当前应用的数据。0为否（所有数据），1为是（仅当前应用），默认为0。</param>
        /// <param name="feature">过滤类型ID，0：全部、1：原创、2：图片、3：视频、4：音乐，默认为0。</param>
        /// <param name="trimUser">返回值中user字段开关，0：返回完整user字段、1：user字段仅返回user_id，默认为0。</param>
        /// <returns></returns>
        public async Task<StatusCollection> GetUserTimelineAsync(long uid = 0, string screenName = null, long sinceId = 0, long maxId = 0, int count = 20, int page = 1, bool baseApp = false, int feature = 0, bool trimUser = false) {
            var json = await HttpHelper.GetBasicCommandAsync("statuses/user_timeline", new WeiboParameter("uid", uid), new WeiboParameter("screen_name", screenName), new WeiboParameter("since_id", sinceId),
                new WeiboParameter("max_id", maxId), new WeiboParameter("count", count), new WeiboParameter("page", page), new WeiboParameter("base_app", baseApp),
                new WeiboParameter("feature", feature), new WeiboParameter("trim_user", trimUser));
            return JsonConvert.DeserializeObject<StatusCollection>(json);
        }
        /// <summary>
        /// 获取用户发布的微博的ID
        /// http://open.weibo.com/wiki/2/statuses/user_timeline/ids
        /// </summary>
        /// <param name="uid">需要查询的用户ID。</param>
        /// <param name="screenName">需要查询的用户昵称。</param>
        /// <param name="sinceId">若指定此参数，则返回ID比since_id大的微博（即比since_id时间晚的微博），默认为0。</param>
        /// <param name="maxId">若指定此参数，则返回ID小于或等于max_id的微博，默认为0。</param>
        /// <param name="count">单页返回的记录条数，最大不超过100，默认为20。</param>
        /// <param name="page">返回结果的页码，默认为1。</param>
        /// <param name="baseApp">是否只获取当前应用的数据。0为否（所有数据），1为是（仅当前应用），默认为0。</param>
        /// <param name="feature">过滤类型ID，0：全部、1：原创、2：图片、3：视频、4：音乐，默认为0。</param>
        /// <returns></returns>
        public async Task<StatusIdCollection> GetUserTimelineIdsAsync(long uid = 0, string screenName = null, long sinceId = 0, long maxId = 0, int count = 20, int page = 1, bool baseApp = false, int feature = 0) {
            var json = await HttpHelper.GetBasicCommandAsync("statuses/user_timeline/ids", new WeiboParameter("uid", uid), new WeiboParameter("screen_name", screenName),
                new WeiboParameter("since_id", sinceId), new WeiboParameter("max_id", maxId), new WeiboParameter("count", count), new WeiboParameter("page", page),
                new WeiboParameter("base_app", baseApp), new WeiboParameter("feature", feature));
            return JsonConvert.DeserializeObject<StatusIdCollection>(json);
        }
        /// <summary>
        /// 获取指定微博的转发微博列表
        /// http://open.weibo.com/wiki/2/statuses/repost_timeline
        /// </summary>
        /// <param name="id">需要查询的微博ID。</param>
        /// <param name="sinceId">若指定此参数，则返回ID比since_id大的微博（即比since_id时间晚的微博），默认为0。</param>
        /// <param name="maxId">若指定此参数，则返回ID小于或等于max_id的微博，默认为0。</param>
        /// <param name="count">单页返回的记录条数，最大不超过100，默认为20。</param>
        /// <param name="page">返回结果的页码，默认为1。</param>
        /// <param name="filterByAuthor">作者筛选类型，0：全部、1：我关注的人、2：陌生人，默认为0。</param>
        /// <returns></returns>
        public async Task<RepostCollection> GetRepostTimelineAsync(long id, long sinceId = 0, long maxId = 0, int count = 20, int page = 1, int filterByAuthor = 0) {
            var json = await HttpHelper.GetBasicCommandAsync("statuses/repost_timeline", new WeiboParameter("id", id), new WeiboParameter("since_id", sinceId),
                new WeiboParameter("max_id", maxId), new WeiboParameter("count", count), new WeiboParameter("page", page),
                new WeiboParameter("filter_by_author", filterByAuthor));
            return JsonConvert.DeserializeObject<RepostCollection>(json);
        }
        /// <summary>
        /// 获取一条原创微博的最新转发微博的ID
        /// http://open.weibo.com/wiki/2/statuses/repost_timeline/ids
        /// </summary>
        /// <param name="id">需要查询的微博ID。</param>
        /// <param name="sinceId">若指定此参数，则返回ID比since_id大的微博（即比since_id时间晚的微博），默认为0。</param>
        /// <param name="maxId">若指定此参数，则返回ID小于或等于max_id的微博，默认为0。</param>
        /// <param name="count">单页返回的记录条数，最大不超过100，默认为20。</param>
        /// <param name="page">返回结果的页码，默认为1。</param>
        /// <param name="filterByAuthor">作者筛选类型，0：全部、1：我关注的人、2：陌生人，默认为0。</param>
        /// <returns></returns>
        public async Task<StatusIdCollection> GetRepostTimelineIdsAsync(long id, long sinceId = 0, long maxId = 0, int count = 20, int page = 1, int filterByAuthor = 0) {
            var json = await HttpHelper.GetBasicCommandAsync("statuses/repost_timeline/ids", new WeiboParameter("id", id), new WeiboParameter("since_id", sinceId), new WeiboParameter("max_id", maxId),
                new WeiboParameter("count", count), new WeiboParameter("page", page), new WeiboParameter("filter_by_author", filterByAuthor));
            return JsonConvert.DeserializeObject<StatusIdCollection>(json);
        }
        /// <summary>
        /// 获取当前用户最新转发的微博列表
        /// http://open.weibo.com/wiki/2/statuses/repost_by_me
        /// </summary>
        /// <param name="sinceId">若指定此参数，则返回ID比since_id大的微博（即比since_id时间晚的微博），默认为0。</param>
        /// <param name="maxId">若指定此参数，则返回ID小于或等于max_id的微博，默认为0。</param>
        /// <param name="count">单页返回的记录条数，最大不超过100，默认为20。</param>
        /// <param name="page">返回结果的页码，默认为1。</param>
        /// <returns></returns>
        public async Task<RepostCollection> GetRepostByMeAsync(long sinceId = 0, long maxId = 0, int count = 20, int page = 1) {
            var json = await HttpHelper.GetBasicCommandAsync("statuses/repost_by_me", new WeiboParameter("since_id", sinceId), new WeiboParameter("max_id", maxId),
                new WeiboParameter("count", count), new WeiboParameter("page", page));
            return JsonConvert.DeserializeObject<RepostCollection>(json);
        }
        /// <summary>
        /// 获取最新的提到登录用户的微博列表，即@我的微博
        /// http://open.weibo.com/wiki/2/statuses/mentions
        /// </summary>
        /// <param name="sinceId">若指定此参数，则返回ID比since_id大的微博（即比since_id时间晚的微博），默认为0。</param>
        /// <param name="maxId">若指定此参数，则返回ID小于或等于max_id的微博，默认为0。</param>
        /// <param name="count">单页返回的记录条数，最大不超过100，默认为20。</param>
        /// <param name="page">返回结果的页码，默认为1。</param>
        /// <param name="filterByAuthor">作者筛选类型，0：全部、1：我关注的人、2：陌生人，默认为0。</param>
        /// <param name="filterBySource">来源筛选类型，0：全部、1：来自微博、2：来自微群，默认为0。</param>
        /// <param name="filterByType">原创筛选类型，0：全部微博、1：原创的微博，默认为0。</param>
        /// <param name="trimUser">返回值中user字段开关，0：返回完整user字段、1：user字段仅返回user_id，默认为0。</param>
        /// <returns></returns>
        public async Task<StatusCollection> GetMentionsAsync(long sinceId = 0, long maxId = 0, int count = 20, int page = 1, int filterByAuthor = 0, int filterBySource = 0, int filterByType = 0, bool trimUser = false) {
            var json = await HttpHelper.GetBasicCommandAsync("statuses/mentions", new WeiboParameter("since_id", sinceId), new WeiboParameter("max_id", maxId), new WeiboParameter("count", count),
                new WeiboParameter("page", page), new WeiboParameter("filter_by_author", filterByAuthor), new WeiboParameter("filter_by_source", filterBySource),
                new WeiboParameter("filter_by_type", filterByType), new WeiboParameter("trim_user", trimUser));
            return JsonConvert.DeserializeObject<StatusCollection>(json);
        }
        /// <summary>
        /// 获取@当前用户的最新微博的ID
        /// http://open.weibo.com/wiki/2/statuses/mentions/ids
        /// </summary>
        /// <param name="sinceId">若指定此参数，则返回ID比since_id大的微博（即比since_id时间晚的微博），默认为0。</param>
        /// <param name="maxId">若指定此参数，则返回ID小于或等于max_id的微博，默认为0。</param>
        /// <param name="count">单页返回的记录条数，最大不超过100，默认为20。</param>
        /// <param name="page">返回结果的页码，默认为1。</param>
        /// <param name="filterByAuthor">作者筛选类型，0：全部、1：我关注的人、2：陌生人，默认为0。</param>
        /// <param name="filterBySource">来源筛选类型，0：全部、1：来自微博、2：来自微群，默认为0。</param>
        /// <param name="filterByType">原创筛选类型，0：全部微博、1：原创的微博，默认为0。</param>
        /// <returns></returns>
        public async Task<StatusIdCollection> GetMentionsIdsAsync(long sinceId = 0, long maxId = 0, int count = 20, int page = 1, int filterByAuthor = 0, int filterBySource = 0, int filterByType = 0) {
            var json = await HttpHelper.GetBasicCommandAsync("statuses/mentions/ids", new WeiboParameter("since_id", sinceId), new WeiboParameter("max_id", maxId), new WeiboParameter("count", count),
                new WeiboParameter("page", page), new WeiboParameter("filter_by_author", filterByAuthor), new WeiboParameter("filter_by_source", filterBySource),
                new WeiboParameter("filter_by_type", filterByType));
            return JsonConvert.DeserializeObject<StatusIdCollection>(json);
        }
        /// <summary>
        /// 获取双向关注用户的最新微博
        /// http://open.weibo.com/wiki/2/statuses/bilateral_timeline
        /// </summary>
        /// <param name="sinceId">若指定此参数，则返回ID比since_id大的微博（即比since_id时间晚的微博），默认为0。</param>
        /// <param name="maxId">若指定此参数，则返回ID小于或等于max_id的微博，默认为0。</param>
        /// <param name="count">单页返回的记录条数，最大不超过100，默认为20。</param>
        /// <param name="page">返回结果的页码，默认为1。</param>
        /// <param name="baseApp">是否只获取当前应用的数据。0为否（所有数据），1为是（仅当前应用），默认为0。</param>
        /// <param name="feature">过滤类型ID，0：全部、1：原创、2：图片、3：视频、4：音乐，默认为0。</param>
        /// <param name="trimUser">返回值中user字段开关，0：返回完整user字段、1：user字段仅返回user_id，默认为0。</param>
        /// <returns></returns>
        public async Task<StatusCollection> GetBilateralTimelineAsync(long sinceId = 0, long maxId = 0, int count = 20, int page = 1, bool baseApp = false, int feature = 0, bool trimUser = false) {
            var json = await HttpHelper.GetBasicCommandAsync("statuses/bilateral_timeline", new WeiboParameter("since_id", sinceId), new WeiboParameter("max_id", maxId),
                new WeiboParameter("count", count), new WeiboParameter("page", page), new WeiboParameter("base_app", baseApp),
                new WeiboParameter("feature", feature), new WeiboParameter("trim_user", trimUser));
            return JsonConvert.DeserializeObject<StatusCollection>(json);
        }
        /// <summary>
        /// 根据微博ID获取单条微博内容
        /// http://open.weibo.com/wiki/2/statuses/show
        /// </summary>
        /// <param name="id">需要获取的微博ID。</param>
        /// <returns></returns>
        public async Task<Status> ShowAsync(long id) {
            var json = await HttpHelper.GetBasicCommandAsync("statuses/show", new WeiboParameter("id", id));
            return JsonConvert.DeserializeObject<Status>(json);
        }
        /// <summary>
        /// 通过微博（评论、私信）ID获取其MID
        /// http://open.weibo.com/wiki/2/statuses/querymid
        /// </summary>
        /// <param name="type">获取类型，1：微博、2：评论、3：私信，默认为1。</param>
        /// /// <param name="ids">需要查询的微博（评论、私信）ID，批量模式下，用半角逗号分隔，最多不超过20个。</param>
        /// <returns></returns>
        public async Task<IEnumerable<string>> QueryMIdAsync(int type = 1, params long[] ids) {
            bool isBatch = ids.Length > 1;
            var json = await HttpHelper.GetBasicCommandAsync("statuses/querymid", new WeiboParameter("id", string.Join(",", ids)), new WeiboParameter("type", type), new WeiboParameter("is_batch", isBatch));
            return Utility.GetStringListFromJson(json);
        }
        /// <summary>
        /// 通过微博（评论、私信）MID获取其ID
        /// http://open.weibo.com/wiki/2/statuses/queryid
        /// </summary>
        /// <param name="type">获取类型，1：微博、2：评论、3：私信，默认为1。</param>
        /// <param name="inbox">仅对私信有效，当MID类型为私信时用此参数，0：发件箱、1：收件箱，默认为0 。</param>
        /// <param name="isBase62">MID是否是base62编码，0：否、1：是，默认为0。</param>
        /// /// <param name="mids">需要查询的微博（评论、私信）MID，批量模式下，用半角逗号分隔，最多不超过20个。</param>
        /// <returns></returns>
        public async Task<IEnumerable<string>> QueryIdAsync(int type = 1, int inbox = 0, bool isBase62 = false, params string[] mids) {
            bool isBatch = mids.Length > 1;
            var json = await HttpHelper.GetBasicCommandAsync("statuses/queryid", new WeiboParameter("mid", string.Join(",", mids)), new WeiboParameter("type", type), new WeiboParameter("is_batch", isBatch),
                new WeiboParameter("inbox", inbox), new WeiboParameter("isBase62", isBase62));
            return Utility.GetStringListFromJson(json);
        }
        /// <summary>
        /// 批量获取指定微博的转发数评论数
        /// http://open.weibo.com/wiki/2/statuses/count
        /// </summary>
        /// <param name="ids">需要获取数据的微博ID，多个之间用逗号分隔，最多不超过100个。</param>
        /// <returns></returns>
        public async Task<IEnumerable<Status_Count>> GetCountsAsync(params long[] ids) {
            var json = await HttpHelper.GetBasicCommandAsync("statuses/count", new WeiboParameter("uids", string.Join(",", ids)));
            return JsonConvert.DeserializeObject<IEnumerable<Status_Count>>(json);
        }
        /// <summary>
        /// 获取微博官方表情的详细信息
        /// http://open.weibo.com/wiki/2/emotions
        /// </summary>
        /// <param name="type"></param>
        /// <param name="language"></param>
        /// <returns></returns>
        public async Task<IEnumerable<Emotion>> GetEmotionsAsync(string type = "face", string language = "cnname") {
            var json = await HttpHelper.GetBasicCommandAsync("emotions", new WeiboParameter("type", type), new WeiboParameter("language", language));
            return JsonConvert.DeserializeObject<IEnumerable<Emotion>>(json);
        }
        #endregion
        #region 写入接口
        /// <summary>
        /// 转发一条微博
        /// http://open.weibo.com/wiki/2/statuses/repost
        /// </summary>
        /// <param name="id">要转发的微博ID。</param>
        /// <param name="status">添加的转发文本，必须做URLencode，内容不超过140个汉字，不填则默认为“转发微博”。</param>
        /// <param name="isComment">是否在转发的同时发表评论，0：否、1：评论给当前微博、2：评论给原微博、3：都评论，默认为0 。</param>
        /// <returns></returns>
        public async Task<Status> RepostAsync(long id, string status = null, int isComment = 0) {
            var json = await HttpHelper.PostBasicCommandAsync("statuses/repost", new WeiboParameter("id", id), new WeiboParameter("status", status), new WeiboParameter("is_comment", isComment));
            return JsonConvert.DeserializeObject<Status>(json);
        }
        /// <summary>
        /// 根据微博ID删除指定微博
        /// http://open.weibo.com/wiki/2/statuses/destroy
        /// </summary>
        /// <param name="id">需要删除的微博ID。</param>
        /// <returns></returns>
        public async Task<Status> DestroyAsync(long id) {
            var json = await HttpHelper.PostBasicCommandAsync("statuses/destroy", new WeiboParameter("id", id));
            return JsonConvert.DeserializeObject<Status>(json);
        }
        /// <summary>
        /// 发布一条新微博
        /// http://open.weibo.com/wiki/2/statuses/update
        /// </summary>
        /// <param name="status">要发布的微博文本内容，必须做URLencode，内容不超过140个汉字。</param>
        /// <param name="visible">微博的可见性，0：所有人能看，1：仅自己可见，2：密友可见，3：指定分组可见，默认为0。</param>
        /// <param name="listId">微博的保护投递指定分组ID，只有当visible参数为3时生效且必选。</param>
        /// <param name="lat">纬度，有效范围：-90.0到+90.0，+表示北纬，默认为0.0。</param>
        /// <param name="longi">经度，有效范围：-180.0到+180.0，+表示东经，默认为0.0。</param>
        /// <param name="annotations">元数据，主要是为了方便第三方应用记录一些适合于自己使用的信息，每条微博可以包含一个或者多个元数据，必须以json字串的形式提交，字串长度不超过512个字符，具体内容可以自定。</param>
        /// <returns></returns>
        public async Task<Status> UpdateAsync(string status, int visible = 0, string listId = null, byte[] pic = null, float lat = 0f, float longi = 0f, string annotations = null) {
            var json = await HttpHelper.PostBasicCommandAsync("statuses/update", new WeiboParameter("status", status), new WeiboParameter("visible", visible), new WeiboParameter("list_id", listId),
                new WeiboParameter("pic", pic), new WeiboParameter("lat", lat), new WeiboParameter("long", longi), new WeiboParameter("annotations", annotations));
            return JsonConvert.DeserializeObject<Status>(json);
        }
        #endregion
    }
}
